<?php
require_once(ABSPATH. 'include/dao/mydao.php');
require_once(ABSPATH. 'include/model/Reply.php');

class ReplyDAO{
	function insert(&$vo){
		 $this->cc($vo);
	     $sql = "insert into replies(post_id,user_id,content,reply_date,status)values(".
	     "'".$vo->post_id."',".
	     "'".$vo-> User->id ."',".
	     "'".$vo->content."',".
	     "now(),".
	     "'0')";	    
	     return mydao_insert($sql);
	}	
	
	function set_status($id,$status){
		$sql="update replies set status=$status where id=$id";
		mydao_runSql($sql);
	}
	
	function get_postId($postId,$offset){
	     $sql="select replies.id,post_id,user_id,content,reply_date,replies.status,users.name,users.profile_image_url".
	     " from replies,users ".
	     " where replies.user_id=users.id and replies.post_id=$postId".
	     " order by replies.id asc ".
		 " LIMIT $offset,20";  	    
	     return $this->convertRows(mydao_getData($sql));	     
	}
	
	private function cc(&$vo){
		$mysql = new SaeMysql();
		$vo->post_id = intval($vo->post_id);		
		$vo->title = $mysql-> escape(strip_tags($vo->content));
	}
	
	private function convert($result){
	     $vo = new Reply();
	     $vo->id = $result["id"];     
	     $vo->post_id = $result["post_id"]; 
	     $vo->content = $result["content"];     
	     $vo->reply_date = $result["reply_date"];     
	     $vo->status = $result["status"];	     
	    
		$vo-> User->id=$result["user_id"];
		$vo-> User->name=$result["name"];
		$vo-> User->profile_image_url=$result["profile_image_url"] ;
		
	     return $vo;
	}
	
	private function convertRows($rows){
		if(!$rows) return false;
		
		$results = array();
		foreach ($rows as $row){
			array_push($results, $this->convert($row));
		}
		return $results;
	}
}
?>